package ma.projet.managedbean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;

import org.hibernate.HibernateException;

import ma.projet.beans.Ordonnance;
import ma.projet.beans.Visite;
import ma.projet.service.OrdonnanceService;
import ma.projet.service.PatientService;
import ma.projet.service.VisiteService;

public class OrdonnanceBean {
	private Ordonnance ordonnance;

	private String idvisite;
	private Visite v;
	private List<Ordonnance> ordonnances;

	public OrdonnanceBean() {
		ordonnance = new Ordonnance();
		v = new Visite();
	}

	public Visite getV() {
		return v;
	}

	public void setV(Visite v) {
		this.v = v;
	}

	public Ordonnance getOrdonnance() {
		return ordonnance;
	}

	public void setOrdonnance(Ordonnance ordonnance) {
		this.ordonnance = ordonnance;
	}

	/**
	 * @return the ordonnances
	 */

	/**
	 * @return the idvisite
	 */
	public String getIdvisite() {
		return idvisite;
	}

	/**
	 * @param idvisite
	 *            the idvisite to set
	 */
	public void setIdvisite(String idvisite) {
		System.out.println(idvisite);
		try {
			v = new VisiteService().getById(Integer.parseInt(idvisite));
			if (idvisite != null)
				remplir(idvisite);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		// v=new VisiteService().getById(i);

		this.idvisite = idvisite;
	}

	public void save() {
		ordonnance.setVisite(v);
		new OrdonnanceService().create(ordonnance);
		// FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove("OrdonnanceBean");
	}

	public List<Ordonnance> getOrdonnances() {

		// FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove("OrdonnanceBean");
		return ordonnances;
	}

	/**
	 * @param ordonnances
	 *            the ordonnances to set
	 */
	public void setOrdonnances(List<Ordonnance> ordonnances) {
		this.ordonnances = ordonnances;
	}

	void remplir(String id) {

		if (ordonnances != null)
			ordonnances = null;
		if (ordonnances == null)
			try {

				ordonnances = new OrdonnanceService().listes(Integer
						.parseInt(id));
				// FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove("OrdonnanceBean");
			} catch (Exception e) {
				System.out.println("Erreur Liste");
			}
		System.out.println("size" + ordonnances.size());
	}
}
